package br.mil.eb.pqrmnt7.scc.persistence.jpa.linhacontrolesaldocombustivel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.mil.eb.pqrmnt7.scc.entity.OrdemAbastecimento;
import br.mil.eb.pqrmnt7.scc.entity.QuotaCombustivel;
import br.mil.eb.pqrmnt7.scc.entity.controleabastecimento.LinhaControleSaldoCombustivel;
import br.mil.eb.pqrmnt7.scc.entity.controleabastecimento.LinhaControleSaldoCombustivelEntity;
import br.mil.eb.pqrmnt7.scc.persistence.jpa.AppJpaDAO;
import br.mil.eb.pqrmnt7.scc.persistence.jpa.ordemabastecimento.OrdemAbastecimentoDAO;

import com.powerlogic.jcompany.commons.PlcBaseContextVO;
import com.powerlogic.jcompany.commons.PlcException;
import com.powerlogic.jcompany.commons.annotation.PlcAggregationDAOIoC;
import com.powerlogic.jcompany.commons.config.stereotypes.SPlcDataAccessObject;
import com.powerlogic.jcompany.persistence.jpa.PlcQuery;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryFirstLine;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryLineAmount;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryOrderBy;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryParameter;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryService;
/**
 * Classe de Persistência gerada pelo assistente
 */

@PlcAggregationDAOIoC(LinhaControleSaldoCombustivelEntity.class)
@SPlcDataAccessObject
@PlcQueryService
public class LinhaControleSaldoCombustivelDAO extends AppJpaDAO  {

	private static final Log logger = LogFactory.getLog(OrdemAbastecimentoDAO.class);	
	
	@Inject
	PlcBaseContextVO context;	
	
	@PlcQuery("querySel")
	public native List<LinhaControleSaldoCombustivelEntity> findList(
			PlcBaseContextVO context,
			@PlcQueryOrderBy String dynamicOrderByPlc,
			@PlcQueryFirstLine Integer primeiraLinhaPlc, 
			@PlcQueryLineAmount Integer numeroLinhasPlc,		   
			
			@PlcQueryParameter(name="data", expression="data >= :data  ") Date data,
			@PlcQueryParameter(name="Entrada", expression="Entrada = :Entrada") Long Entrada,
			@PlcQueryParameter(name="Saida", expression="Saida = :Saida") Long Saida,
			@PlcQueryParameter(name="saldo", expression="saldo = :saldo") Long saldo,
			@PlcQueryParameter(name="viatura", expression="viatura like :viatura || '%' ") String viatura,
			@PlcQueryParameter(name="motorista", expression="motorista like :motorista || '%' ") String motorista,
			@PlcQueryParameter(name="numeroVale", expression="numeroVale like :numeroVale || '%' ") String numeroVale,
			@PlcQueryParameter(name="fonte", expression="fonte like :fonte || '%' ") String fonte
	);

	@PlcQuery("querySel")
	public native Long findCount(
			PlcBaseContextVO context,
			
			@PlcQueryParameter(name="data", expression="data >= :data  ") Date data,
			@PlcQueryParameter(name="Entrada", expression="Entrada = :Entrada") Long Entrada,
			@PlcQueryParameter(name="Saida", expression="Saida = :Saida") Long Saida,
			@PlcQueryParameter(name="saldo", expression="saldo = :saldo") Long saldo,
			@PlcQueryParameter(name="viatura", expression="viatura like :viatura || '%' ") String viatura,
			@PlcQueryParameter(name="motorista", expression="motorista like :motorista || '%' ") String motorista,
			@PlcQueryParameter(name="numeroVale", expression="numeroVale like :numeroVale || '%' ") String numeroVale,
			@PlcQueryParameter(name="fonte", expression="fonte like :fonte || '%' ") String fonte
	);
	
	public List<LinhaControleSaldoCombustivel> recuperaPorPeriodo(Date dataInicio,	Date dataFim) {
		try {
			
			logger.debug("Chegou ao recupera no LinhaSaldoCombustivelDAO");
			
			EntityManager entityManager = getEntityManager(context);
			
			logger.debug("dataInicio: " + dataInicio);
			logger.debug("dataFim: " + dataFim);
			
			List<OrdemAbastecimento> ordens = entityManager.createNamedQuery("OrdemAbastecimentoEntity.recuperaPorPeriodo")
					   .setParameter("dataInicio",dataInicio)
					   .setParameter("dataFim",dataFim)
				       .getResultList();
			
			logger.debug("Número de entidades OrdemAbastecimento recuperadas: " + ordens.size());
			
			List<QuotaCombustivel> quotas = entityManager.createNamedQuery("QuotaCombustivelEntity.recuperaPorPeriodo")
					   .setParameter("dataInicio",dataInicio)
					   .setParameter("dataFim",dataFim)
				       .getResultList();
			
			logger.debug("Número de entidades QuotaCombustivel recuperadas: " + quotas.size());
			
			List<LinhaControleSaldoCombustivel> linhas = new ArrayList<LinhaControleSaldoCombustivel>();
			
			for (OrdemAbastecimento ordem : ordens) {
				linhas.add(new LinhaControleSaldoCombustivelEntity(ordem));
			}
			
			for (QuotaCombustivel quota : quotas) {
				linhas.add(new LinhaControleSaldoCombustivelEntity(quota));
			}
			
			return linhas;
			
		} catch (Exception e) {
			logger.error(e);
			throw new PlcException("LinhaSaldoCombustivelDAO","recuperaPorPeriodo",e,log,"");
		}
	}	

	
	
}
